var express = require('express');
var MerchantService = require('../server/service/merchant.service');
var session = require('express-session');
var router = express.Router();

router.post('/', function (req, res) {
    let name = req.body.name;
    let password = req.body.password;
    console.log(name);
    console.log('[用户登录]:' + name + '--' + password);
    MerchantService.login(name, password, function (doc) {
        if (doc.status == true) {
            console.log('[登录成功]');
            doc.data.password = '';
            req.session.user = doc.data;
            console.log('[当前用户]' + req.session.user.merchantName);
        }
        res.send(doc);
    });
});

router.get('/logout', function (req, res) {
    if (req.session.user) {
        console.log('[用户登出]:', req.session.user.merchantName);
        req.session.user = undefined;
    }
    res.end();
});

module.exports = router;
